<?php

namespace App\Http\Controllers;

use App\Topic;
use Illuminate\Http\Request;

class TopicController extends Controller
{

    //专题页
    public function index(Topic $topic)
    {
        //带文章数目的专题
        $topic =  $topic->withCount('postTopics')->find($topic->id);

        //专题下文章
        $posts = $topic->posts()->orderBy('created_at','desc')->paginate(10);

        //属于我的文章，但是未投稿

        $myposts = \App\Post::authorBy(\Auth::id())->topicNotBy($topic->id)->get();

        return view('topic.index',compact('topic','posts','myposts'));
    }

    //专题投稿
    public function submit(Topic $topic)
    {
        $this->validate(request(), [
            'post_ids' => 'required|array',
        ]);

        $post_ids = request('post_ids');
        $topic_id = $topic->id;
        foreach ($post_ids as $post_id) {
            \App\PostTopic::firstOrCreate(compact('topic_id','post_id'));
        }

        return back();
    }
}
